package mest.controller.cxj;

import com.alibaba.fastjson.JSON;
import mest.VO.cxj.CostOrderVO;
import mest.entity.DictionariesOrder;
import mest.entity.costorder;
import mest.service.cxj.CostOrderService;
import mest.service.cxj.DictionariesOrderService;
import mest.service.system.DBService;
import mest.util.NumberConst;
import mest.util.ResultObj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("costOrder")
@CrossOrigin
public class CostOrderController {

    @Autowired
    private CostOrderService costOrderService;

    @Autowired
    private DictionariesOrderService dictionariesOrderService;

    @Autowired
    private DBService dbService;


    /**
     * 生成费用单编码
     *
     * @return
     */
    @GetMapping("getCode")
    public String getCode() {
        String theirDb=dbService.getCompanyDB();
        DictionariesOrder dictionariesOrder = dictionariesOrderService.selectDictionariesOrder(theirDb);
        int costOrderNumber = dictionariesOrder.getCostOrderNumber();
        return NumberConst.CostOrder_No + "-" + costOrderNumber;
    }

    /**
     * 新增费用单
     *
     * @param co
     * @return
     */
    @PostMapping("insert")
    public String insert(@RequestBody costorder co) {
        ResultObj resultObj = null;
        String theirDb = dbService.getCompanyDB();
        try {
            co.setEfId(1);//设置默认操作人
            if (costOrderService.insertCostOrder(co, theirDb) > 0) {
                dictionariesOrderService.updateDictionariesOrderByPlanCostOrderNumber(theirDb);
                resultObj = ResultObj.builder().code(0).msg("添加成功").build();
            } else {
                resultObj = ResultObj.builder().code(-1).msg("添加失败").build();
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultObj = ResultObj.builder().code(-2).msg("系统异常,请联系管理员").build();
        }
        return JSON.toJSONString(resultObj);
    }


    /**
     * 修改费用单
     *
     * @param co
     * @return
     */
    @PostMapping("modify")
    public String modify(@RequestBody costorder co) {
        ResultObj resultObj = null;
        String theirDb = dbService.getCompanyDB();
        try {
            if (costOrderService.modifyCostOrder(co, theirDb) > 0) {
                resultObj = ResultObj.builder().code(0).msg("修改成功").build();
            } else {
                resultObj = ResultObj.builder().code(-1).msg("修改失败").build();
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultObj = ResultObj.builder().code(-2).msg("系统异常,请联系管理员").build();
        }
        return JSON.toJSONString(resultObj);
    }


    /**
     * 查询全部费用单
     *
     * @param page
     * @param limit
     * @param content
     * @return
     */
    @GetMapping("selectAll")
    public String selectAll(@RequestParam Integer page, @RequestParam Integer limit,
                            @RequestParam(required = false) String content) {
        ResultObj resultObj = null;
        String theirDb = dbService.getCompanyDB();
        try {
            List<CostOrderVO> costOrderVOList = costOrderService.selectAllCostOrder(page, limit, content, theirDb);
            Integer count = costOrderService.selectCountCostOrder(content, theirDb);
            resultObj = new ResultObj(0, "success", count, costOrderVOList);
        } catch (Exception e) {
            e.printStackTrace();
            resultObj = ResultObj.builder().code(-2).msg("系统异常,请联系管理员").build();
        }
        return JSON.toJSONString(resultObj);
    }

    /**
     * 删除费用单（假删）
     *
     * @param id
     * @return
     */
    @DeleteMapping("delete")
    public String delete(@RequestParam Integer id) {
        ResultObj resultObj = null;
        String theirDb = dbService.getCompanyDB();
        try {
            if (costOrderService.deleteCostOrder(id, theirDb) > 0) {
                resultObj = ResultObj.builder().code(0).msg("删除成功").build();
            } else {
                resultObj = ResultObj.builder().code(-1).msg("删除失败").build();
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultObj = ResultObj.builder().code(-2).msg("系统异常,请联系管理员").build();
        }
        return JSON.toJSONString(resultObj);
    }

}
